#! /usr/bin/env	python
#encoding=utf-8
#生成订单评论表数据

import MySQLdb,string,os,sys,random

def random_str(randomlength=8):
    str = ''
    #chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    chars ='abcdefghijklmonpqrstuvwxyz0123456789'
    length = len(chars) - 1
    randoms = random.Random()
    for i in range(randomlength):
        str+=chars[randoms.randint(0, length)]
    return str
try:
	conn = MySQLdb.connect(host ='10.102.107.22',user ='dba',passwd = '1q2w3e4r',db = 'mc_orderdb',charset="utf8")
except MySQLdb.Error, e:
	print "Error %d: %s \n" % (e.args[0], e.args[1])

for i in range(0,10000):
	#随机获取订单信息
	sqlstr="""
		SELECT beanTwo.`product_id`,beanTwo.`order_id`,beanOne.`customer_id`
			FROM mc_orderdb.order_master beanOne
			JOIN mc_orderdb.`order_detail` beanTwo ON beanOne.`order_id`=beanTwo.`order_id`
			ORDER BY RAND()
			LIMIT 1
	"""
	cursor= conn.cursor(MySQLdb.cursors.DictCursor)
	cursor.execute(sqlstr)
	result = cursor.fetchall()
	for row in result:
		productid=row["product_id"]
		orderid=row["order_id"]
		customerid=row["customer_id"]
	title=random_str(10)
	content=random_str(100)
	#生成评论数据
	sqlstr="""
	INSERT INTO mc_productdb.product_comment(product_id,order_id,customer_id,title,content,audit_status)
	values(%s,%s,%s,'%s','%s',1)
	"""%(productid,orderid,customerid,title,content)

	cursor.execute(sqlstr)
	cursor.execute('commit;')